<?php

namespace app\api\controller;

use app\api\model\ActivityModel;
use app\api\model\ActivityRegisterModel;
use app\BaseController;
use think\Exception;
use think\Model;

class Activity extends BaseController
{
    public function index()
    {
        try {
            $model = new ActivityModel();
            // 1: 查询活动数据
            $activities = $model->where('status', 1)
                ->field('id, title, content, image')
                ->order('id asc')
                ->select()
                ->toArray();
            // 2: 查询并添加报名人数到活动数据
            foreach ($activities as &$activity) {
                $registerModel = new ActivityRegisterModel();
                $count = $registerModel->where('activity_id', $activity['id'])->count();
                $activity['numbers'] = $count;
            }
            $this->success('获取成功', $activities);
        } catch (Exception $e) {
            $this->error('', $e->getMessage());
        }
    }
}